package com.marvin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marvin.core.Blog;
import com.marvin.mapper.BlogMapper;
import com.marvin.service.IArchiveService;
import com.marvin.service.IBlogService;
import com.marvin.util.DateUtil;
import com.marvin.vo.ArticleWithYear;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yinyin
 * @create 2021/12/06 上午 10:15
 */
@Service
public class ArchiveServiceImpl implements IArchiveService {
    @Autowired
    private IBlogService blogService;
    @Resource
    private BlogMapper blogMapper;

    //通过stream进行过滤
    @Override
    public TreeMap<Integer, List<Blog>> queryBlogs() {

        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(Blog::getId,Blog::getTitle,Blog::getCreateTime);
        List<Blog> blogList = blogService.list(queryWrapper);
        //使用stream的分组功能进行数据处理
        TreeMap<Integer, List<Blog>> collect1=new TreeMap<>(((o1, o2) -> o2-o1));
        //返回的map是无序的,treemap的序号要在初始化时定义好,所以我们需要把collect2的倒到collect1中,这样就可以有顺序了,还可以进行排序
        TreeMap<Integer, List<Blog>> collect2 = blogList.stream()
                .collect(Collectors.groupingBy(blog ->blog.getCreateTime().getYear()+1900, TreeMap::new, Collectors.toList()));
        for (Map.Entry<Integer,List<Blog>> integerListEntry:collect2.entrySet()){
            collect1.put(integerListEntry.getKey(),integerListEntry.getValue());
            System.out.println(collect1);
        }
        return collect1;
    }

    //使用对象将其包裹,方便前端取值
    @Override
    public  List<ArticleWithYear>  queryBlogs2(){
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(Blog::getId,Blog::getTitle,Blog::getCreateTime);
        List<Blog> blogList = blogService.list(queryWrapper);
        //返回无序的map
        Map<Integer, List<Blog>> collect = blogList.stream().collect(Collectors.groupingBy(blog -> blog.getCreateTime().getYear() + 1900));
        List<ArticleWithYear> collect1 = collect.entrySet().stream().map(entry -> {
            ArticleWithYear articleWithYear = new ArticleWithYear();
            articleWithYear.setYear(entry.getKey());
            //使用map将其过滤,去掉不用显示的字段
            List<Blog> blogs = entry.getValue();
            List<Map<String, Object>> collect2 = blogs.stream().map(blog -> {

                Map<String, Object> map = new HashMap<>();
                map.put("id", blog.getId());
                map.put("title", blog.getTitle());
                map.put("cTime", blog.getCreateTime());
                return map;
            }).collect(Collectors.toList());
            articleWithYear.setArticleList(collect2);
            return articleWithYear;
        }).sorted((a, b) -> b.getYear() - a.getYear()).collect(Collectors.toList());
        return collect1;
    }

    //查询blog的年份
    @Override
    public List<Map<String,Object>> queryYear(){
        List<String> yearList= blogMapper.queryYear();

        return  yearList.stream().map(year->{
            Map<String,Object> map=new HashMap<>();
            IPage<Blog> childrenBlogByYear= this.getBlogYear(1,10,Integer.parseInt(year));
            map.put("year",year);
            map.put("blogPages",childrenBlogByYear);
            return map;
        }).collect(Collectors.toList());
    }

    //根据年份查询blog并分页
    @Override
    public IPage<Blog> getBlogYear(Integer pageNo,Integer pageSize,Integer year){
        Page<Blog> page = new Page<>(pageNo,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("DATE_FORMAT( kb.create_time, '%Y' )",year);
        queryWrapper.eq("kb.is_delete",0);
        queryWrapper.eq("kb.status",1);
        queryWrapper.orderByDesc("kb.create_time");
        IPage<Blog> childrenBlogByYear = blogMapper.queryBlogByYear(page, queryWrapper);
        return childrenBlogByYear;
    }

}
